package com.dodock.banglapapers.server.helper;

import java.util.HashMap;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsListPoriborton implements DDNNewsListInterface {
	
	HashMap<String, String> newsMap;
	
	@Override
	public String GenerateNewsList(String categoryid) {
		newsMap = new HashMap<String, String>();
		try {
			String url = "";
			if (categoryid.equalsIgnoreCase("latest")) {
				url = Settings.WWW_PORIBORTON_COM + "/index.php";
			}else if(categoryid.equalsIgnoreCase("cricket")||categoryid.equalsIgnoreCase("football")){
				url = Settings.WWW_PORIBORTON_COM + "/article_type.php?article_type=sports&article_in="+categoryid;
			}else{
				url = Settings.WWW_PORIBORTON_COM + "/article_type.php?article_type="+categoryid;
			}
			return getNewsList(url, categoryid);
		} catch (Exception ex) {
			System.out.print("exeception : " + ex.toString());
			return "[]";
		}
	}

	private String getNewsList(String url, String categoryid) {
		System.out.println(url);
		JsonArray array = new JsonArray();
		Document doc;
		try {
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(60000).execute();
			doc = response.parse();
			if (categoryid.equalsIgnoreCase("latest")) {
				generateLatestNewsList(doc, array);
			}else if(categoryid.equalsIgnoreCase("football")||categoryid.equalsIgnoreCase("cricket")){
				generateSportsNewsList(doc, array);
			}else if(categoryid.equalsIgnoreCase("entertainment")){
				generateEnertainmentNewsList(doc, array);
			}else {
				generateCategoryNewsList(doc, array);
			}
			System.out.println(array.size());
			
			
			return array.toString();
		} catch (Exception e) {
			System.out.println(e.toString());
			return "[]";
		}
	}
	
	private void generateEnertainmentNewsList(Document doc, JsonArray array) {
		System.out.println("entertainment news list");
		Element element = doc.select("div[class=nivoSlider]").first();
		Elements newsElementObjs = element.select("a");
		for(Element newsElements : newsElementObjs){
			parseHolderNewsElement(newsElements, array);
		}
		
		newsElementObjs = doc.select("div[class=shFetaureFeatureCt]");
		for(Element newsElements : newsElementObjs){
			parseCategoryNewsElement(newsElements, array);
		}
		
		newsElementObjs = doc.select("div[class=shFetaureAllBottomText]");
		for(Element newsElements : newsElementObjs){
			parseCategoryNewsElement(newsElements, array);
		}
	}
	
	
	private void generateSportsNewsList(Document doc, JsonArray array) {
		Element element = doc.select("div[class=nivoSlider]").first();
		Elements leadNewsElementObjs = element.select("a");
		for(Element newsElements : leadNewsElementObjs){
			parseHolderNewsElement(newsElements, array);
		}
		
		leadNewsElementObjs = doc.select("div[class=shSportsFeatureCt]");
		for(Element newsElements : leadNewsElementObjs){
			parseCategoryNewsElement(newsElements, array);
		}
		
		
		element = doc.select("div[class=shMSportsRightfeature").first();
		leadNewsElementObjs = element.select("a");
		for(Element newsElements : leadNewsElementObjs){
			parseHolderNewsElement(newsElements, array);
		}
	}
	
	private void generateCategoryNewsList(Document doc, JsonArray array) {
	System.out.println("loading list");
		Elements leadNewsElementObjs = doc.select("div[class=shArtCultureLeadCt]");
		if(!leadNewsElementObjs.isEmpty()){
			parseCategoryNewsElement(leadNewsElementObjs.first(), array);
		}
		
		Elements newsElementObjs = doc.select("div[class=news-box]");
		for(Element newsElements : newsElementObjs){
			parseCategoryNewsElement(newsElements, array);
		}
	}
	
	private void generateLatestNewsList(Document doc, JsonArray array) {
		Elements galleryNewsObj = doc.select("div[class=shContentGallerySlide]");
		for(Element galleryNews : galleryNewsObj){
			parseGalleryNewsElement(galleryNews, array);
		}

		Element boxNewsElements = doc.select("div[class=shMainBodyHotNewsLeftCt pori-list]").first();
		Elements boxNewsElementObjs = boxNewsElements.select("a");
		for(Element boxNewElement : boxNewsElementObjs){
			parseElement(boxNewElement, array);
		}
		
		Element slideNewsObjs = doc.select("div[class=shHomeTodaysNews]").first();
		Elements slideNewsObjList = slideNewsObjs.select("a");
		for(Element slideNews : slideNewsObjList){
			parseElement(slideNews, array);
		}

		
	}
	
	private void parseElement(Element element, JsonArray array){
		JsonObject obj = new JsonObject();
		String id = element.attr("href").substring(1);		
		String title = element.text();
		
		Elements imageElements = element.select("img");
		if(!imageElements.isEmpty()){
			String image = element.select("img").first().attr("src").substring(1);
			obj.addProperty("image",Settings.WWW_PORIBORTON_COM + image);
		}
		obj.addProperty("id", encode(id));
		obj.addProperty("title", title);
		if(!newsMap.containsKey(id)){
			newsMap.put(id, id);
			array.add(obj);
		}
		
	}
	
	private void parseGalleryNewsElement(Element element, JsonArray array){
		JsonObject obj = new JsonObject();
		String id = element.select("a").first().attr("href").substring(1);
		Elements imageElements = element.select("img");
		if(!imageElements.isEmpty()){
			String image = element.select("img").first().attr("src").substring(1);
			obj.addProperty("image", Settings.WWW_PORIBORTON_COM + image);
			String title = element.select("img").first().attr("alt");
			obj.addProperty("title", title);
		}
		obj.addProperty("id", encode(id));
		if(!newsMap.containsKey(id)){
			newsMap.put(id, id);
			array.add(obj);
		}
	}
	
	private void parseCategoryNewsElement(Element element, JsonArray array){
		JsonObject obj = new JsonObject();
		String id = element.select("a").first().attr("href").substring(1);
		Elements imageElements = element.select("img");
		obj.addProperty("id", encode(id));
		System.out.println(encode(id));
		if(!imageElements.isEmpty()){
			String image = element.select("img").first().attr("src").substring(1);
			System.out.println(Settings.WWW_PORIBORTON_COM + image);
			obj.addProperty("image",Settings.WWW_PORIBORTON_COM + image);
			String title = element.select("img").first().attr("title");
			obj.addProperty("title", title);
			System.out.println(title);
			if(!newsMap.containsKey(id)){
				newsMap.put(id, id);
				array.add(obj);
			}
		}
	}
	
	private void parseHolderNewsElement(Element element, JsonArray array){
		JsonObject obj = new JsonObject();
		String id = element.attr("href").substring(1);
		Elements imageElements = element.select("img");
		if(!imageElements.isEmpty()){
			String image = element.select("img").first().attr("src").substring(1);
			obj.addProperty("image",Settings.WWW_PORIBORTON_COM + image);
			String title = element.select("img").first().attr("title");
			obj.addProperty("title", title);
			obj.addProperty("id", encode(id));
			if(!newsMap.containsKey(id)){
				newsMap.put(id, id);
				array.add(obj);
			}
		}
	}
	
	private String encode(String id){
		id = id.replaceAll(" ", "%20");
		id = id.replaceAll("&", "%26");
		return id;
	}
}
